<?php

namespace app\admin\services;

use think\Service;
use think\facade\Request;
use app\admin\model\AdminmanualModel;

/**
 * @后台手册管理
 */
class AmindmanualService extends Service
{
    protected $manualModel;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->manualModel = new AdminmanualModel();
    }
    
    /**
     * @保存数据
     */
    public function saveData(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        
        $where=[];
        empty($post['id'])?$where=[]:$where=['id'=>$post['id']];
        
        if(empty($post['title'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        if(empty($post['sort']) && $post['sort'] != '0'){
            return $this->_getResponse("PARAM_EORRE");
        }
        if(empty($post['status'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        if(empty($post['contents'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        
        $post['contents'] = htmlspecialchars($post['contents'],ENT_QUOTES);
       
        $data = $this->manualModel->saveData($post,$where);
        
        if(!$data){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }
    
    /**
     * @修改状态
     */
    public function editStatus(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        
        $where=[];
        empty($post['id'])?$where=[]:$where=['id'=>$post['id']];
        
        if(empty($post['status'])){
            return $this->_getResponse("PARAM_EORRE");
        }
       
        $data = $this->manualModel->saveData($post,$where);
        
        if(!$data){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }
    
    /**
     * @获取列表分页数据
     * @param int $pageIndex 当前页码
     * @param int $pageSize 每页条数
     * @param string $keyword 关键字查询
     * @param int $groupId 分类ID
     */
    public function getListByPage(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        
        (!empty($post['title']))?$where[] =['title','like','%'.$post['title'].'%']:$where[]=['status','neq',-1];
        
        if(!empty($post['group']) && $post['group'] != '0'){
            $where[]=['group','eq',$post['group']];
        }
        
        $ruslt = $this->manualModel->selectDataByPage($page,$limit,$where);
        
        $counts = $this->manualModel->getCount($where);
        
        $data['rel'] = true;
        $data['msg'] = "获取成功";
        $data['list'] = $ruslt;
        $data['count'] = $counts;
        return $data;
    }
    
    /**
     * @获取详情
     */
    public function getDataById(){
        $post = input('get.');
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $ruslt = $this->manualModel->selectDataById($post['id']);
        $ruslt['contents'] = html_entity_decode($ruslt['contents'],ENT_QUOTES);
        return $ruslt;
    }
    
    /**
     * @关键字查询
     */
    public function selectLikeList(){
        $post = input('get.');
        
        if(empty($post['pid'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $ruslt = $this->manualModel->selectLikeList($post['pid'],$post['keyword']);
        return $ruslt;
    }
    
    /**
     * @删除
     * @param int $id ID
     */
    public function del(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        
        $where=[];
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }else{
            $where=['id'=>$post['id']];
        }
        
        $data = $this->manualModel->del($where);
        
        if(!$data){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        
        return $this->_getResponse("DELETE_DATE");
    }

    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "ROLE_ERROR" => ["status" => 7, "msg" => "该角色已经存在", "data" => $data]
        ];
        return $maps[$key];
    }
    
    
    
    /**
     * @param 上传图片
     */
    public function ajaxUpload(){
        $type = strtolower(substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1));
        $allow_type = array('jpg','jpeg','gif','png');
        if(!in_array($type,$allow_type)) {
            return false;
        }
        
        $filename = time().'.'.$type;
        $paths = './static/upload/manual';
        if(!file_exists($paths)){
            mkdir($paths,0777);
        }
        
        $path =$paths."/".$filename;
        $result = move_uploaded_file($_FILES["file"]["tmp_name"],$path);
        
        if($result){
            return ['code'=>0,'msg'=>'上传成功','data'=>['src'=>'/static/upload/manual/'.$filename,'title'=>$filename]];
        }else{
            return ['code'=>1,'msg'=>'上传错误'];
        }
    }

}